package com.example.androiddevchallenge

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.Spring.StiffnessLow
import androidx.compose.animation.core.spring
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.androiddevchallenge.screens.WELCOME_SCREEN
import com.example.androiddevchallenge.screens.WelcomeScreen
import com.example.androiddevchallenge.screens.home.HOME_SCREEN
import com.example.androiddevchallenge.screens.home.HomeScreen
import com.example.androiddevchallenge.screens.login.LOGIN_SCREEN
import com.example.androiddevchallenge.screens.login.LoginScreen
import com.example.androiddevchallenge.ui.theme.BloomSecondaryPrimaryTheme
import com.example.androiddevchallenge.ui.theme.MyTheme
import dev.chrisbanes.accompanist.insets.ProvideWindowInsets
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

class ScreensActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        WindowCompat.setDecorFitsSystemWindows(window, false)

        setContent {
            MyTheme {
                ProvideWindowInsets {
                    ScreenApp()
                }
            }
        }
    }
}

@Composable
fun ScreenApp() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = WELCOME_SCREEN) {
        composable(WELCOME_SCREEN) { WelcomeScreen(navController = navController) }
        composable(LOGIN_SCREEN) {
            BloomSecondaryPrimaryTheme {
                LoginScreen(navController = navController)
            }
        }
        composable(HOME_SCREEN) {
            HomeScreen(viewModel = viewModel())
        }
    }
}

@Preview
@Composable
fun LoginPage() {
    val animatedDegree = remember { Animatable(0f) }
    val animatedScale = remember { Animatable(1f) }
    Box(contentAlignment = Alignment.Center) {
        val asAndroidBitmap = ImageBitmap.imageResource(id = R.drawable.jetpack_1).asAndroidBitmap()
        Image(
            bitmap = BitmapBlur.doBlur(asAndroidBitmap, animatedDegree.value.toInt() + 20, false)
                .asImageBitmap(),
            contentDescription = "",
            contentScale = ContentScale.FillHeight,
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .scale(animatedScale.value, animatedScale.value)
        )

        //圆形图片和圆形弧圈
        Column(verticalArrangement = Arrangement.Center, modifier = Modifier.pointerInput(Unit) {
            coroutineScope {
                while (true) {
                    //2.通过awaitPointerEventScope来处理指针事件
                    val offset = awaitPointerEventScope {
                        //获取第一次按下的位置坐标
                        awaitFirstDown().position

                    }
                    //携程
                    launch {
                        //3.设置animated的目标值为按下的屏幕坐标系内的x值,且设置动画格式为比较平缓不生硬。开始执行动画。
                        animatedDegree.animateTo(
                            offset.x,
                            animationSpec = spring(stiffness = StiffnessLow)
                        )

                    }

                }
            }
        }) {
            Image( bitmap = ImageBitmap.imageResource(R.drawable.jetpack_1),
                contentDescription = "w",
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .height(80.dp)
                    .width(80.dp)
                    .background(color = Color(0XFF0DBEBF), shape = CircleShape)
                    .padding(3.dp)
                    .clip(
                        CircleShape
                    )
                    .shadow(elevation = 150.dp, clip = true)
                    .rotate(//4.设置角度为初始化到目标x的动画值跟新UI
                        animatedDegree.value
                    )
            )
        }
    }
}

